//
//  NBCURLRouter.m
//  NBCURLRouterDemo
//
//  Created by naruto on 16/8/17.
//  Copyright © 2016年 naruto. All rights reserved.
//  

#import "NBCURLRouter.h"
#import "NBCURLNavgation.h"
#import "NBCURLPageModel.h"


@interface NBCURLRouter()

@property (nonatomic, strong) NBCURLNavgation                           *urlNavgation;
/**
 存放page-scheme 配置
 */
@property(nonatomic, strong) NSMutableArray<NBCURLPageModel *>          *routeConfig;

@end

@implementation NBCURLRouter
#pragma mark - system
static NBCURLRouter     *kNBCURLRouterInstance = nil;
+ (NBCURLRouter *)shareURLRouter
{
    static dispatch_once_t kURLRouterOnceToken;
    dispatch_once(&kURLRouterOnceToken, ^{
        if(!kNBCURLRouterInstance){
            kNBCURLRouterInstance = [[NBCURLRouter alloc] init];
        }
    });
    return kNBCURLRouterInstance;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        _urlNavgation = [NBCURLNavgation new];
        _routeConfig = [[NSMutableArray alloc] init];
    }
    return self;
}


#pragma mark - 注册 vc的scheme
-(void)registerScheme:(NSString *)vcClass scheme:(NSString *)scheme
{
    NBCURLPageModel *pageModel = [NBCURLPageModel new];
    pageModel.pageSpace = kNBCURLPageModel_DefaultSpace;
    pageModel.pageClass = vcClass;
    pageModel.scheme = scheme;
    [self.routeConfig addObject:pageModel];
}

#pragma mark - push vc
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [self.urlNavgation pushViewController:viewController animated:animated replace:NO];
}

- (void)pushURLString:(NSString *)urlString animated:(BOOL)animated
{
    UIViewController *viewController = [self.urlNavgation setupFromString:urlString fromConfig:self.routeConfig];
    [self.urlNavgation pushViewController:viewController animated:animated replace:NO];
}

- (void)pushURLString:(NSString *)urlString query:(NSDictionary *)query animated:(BOOL)animated
{
    UIViewController *viewController = [self.urlNavgation setupFromString:urlString withQuery:query fromConfig:self.routeConfig];    
    [self.urlNavgation pushViewController:viewController animated:animated replace:NO];
}



#pragma mark - pop vc
- (void)popViewControllerAnimated:(BOOL)animated
{
    [self.urlNavgation popViewControllerAnimated:animated];
}
- (void)popToRootViewControllerAnimated:(BOOL)animated
{
    [self.urlNavgation popToRootViewControllerAnimated:animated];
}


#pragma mark - present vc
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)animated completion:(void (^ __nullable)(void))completion
{
    [self.urlNavgation presentViewController:viewControllerToPresent animated:animated completion:completion];
}

- (void)presentURLString:(NSString *)urlString animated:(BOOL)animated completion:(void (^ __nullable)(void))completion
{
    UIViewController *viewController = [self.urlNavgation setupFromString:urlString fromConfig:self.routeConfig];
    [self.urlNavgation presentViewController:viewController animated:animated completion:completion];
}
- (void)presentURLString:(NSString *)urlString query:(NSDictionary *)query animated:(BOOL)animated completion:(void (^ __nullable)(void))completion
{
    UIViewController *viewController = [self.urlNavgation setupFromString:urlString withQuery:query fromConfig:self.routeConfig];
    [self.urlNavgation presentViewController:viewController animated:animated completion:completion];
}


#pragma mark - dismiss vc
- (void)dismissViewControllerAnimated: (BOOL)animated completion: (void (^ __nullable)(void))completion
{
    [self.urlNavgation dismissViewControllerWithTimes:1 animated:animated completion:completion];
}

- (void)dismissToRootViewControllerAnimated: (BOOL)flag completion: (void (^ __nullable)(void))completion
{
    [self.urlNavgation dismissToRootViewControllerAnimated:flag completion:completion];
}


@end
